package com.hjjt.logger

import android.util.Log
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.Logger
import com.elvishew.xlog.XLog
import com.elvishew.xlog.flattener.PatternFlattener
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy
import com.elvishew.xlog.printer.file.naming.DateFileNameGenerator
import com.hjjt.baselib.provider.BaseContentContextProvider
import java.io.File

/**
 *
 * @ProjectName:    PluginDemo
 * @Package:        com.hjjt.logger
 * @ClassName:      Logger‘
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/6/13 9:11
 */
object LoggerInstance {
    val MAX_TIME = 1000 * 60 * 60 * 24 * 7L
    fun initLogger(tagName: String): Logger? {
        XLog.init(LogLevel.ALL)
        val androidPrinter = AndroidPrinter(true)
        if (BaseContentContextProvider.mContext == null) return null
        val dirPath =
            BaseContentContextProvider.mContext!!.filesDir.absolutePath + File.separator + "log"
        val dirFile = File(dirPath)
        if (!dirFile.exists()) dirFile.mkdirs()
        val filePrinter =
            FilePrinter.Builder(dirPath)
                .fileNameGenerator(DateFileNameGenerator())
                .flattener(PatternFlattener("{d yyyy-MM-dd hh:mm:ss.SSS} {l}/{t}: {m}"))
                .cleanStrategy(FileLastModifiedCleanStrategy(MAX_TIME)).build()
        return XLog.logLevel(LogLevel.ALL).tag(tagName).enableThreadInfo()
            .enableStackTrace(2)
            .enableBorder().printers(androidPrinter, filePrinter).build()
    }
}

val Any.logger:Logger?
    get() =  null